package com.cn.kabalifeuser.ui.my.activity;


import com.cn.kabalifeuser.api.Api;
import com.cn.kabalifeuser.base.BasePresenter;
import com.cn.kabalifeuser.callback.CacheCallBack;
import com.cn.kabalifeuser.config.Constants;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.ui.integral.bean.DefaultAddressBean;
import com.cn.kabalifeuser.ui.login.bean.MsgBean;
import com.cn.kabalifeuser.ui.my.bean.AddressListBean;
import com.cn.kabalifeuser.ui.my.bean.AddressProvinceListBean;
import com.cn.kabalifeuser.utils.LoggerUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

/**
 * Created by 晓林 on 2020/7/8.
 * function:
 */
public class IAddressPresenter extends BasePresenter<IAddressView> {
    public void onAddressData(String token, int id) {
        OkGo.<DefaultAddressBean>post(Api.API_ADDRESS)
                .headers("token", token)
                .params("id", id)
                .execute(new CacheCallBack<DefaultAddressBean>() {
                    @Override
                    public void onSuccess(Response<DefaultAddressBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("默认地址：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onAddressSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    public void onAddressListData(String token, int page, int limit) {
        OkGo.<AddressListBean>post(Api.API_USER_ADDRESS_LIST)
                .headers("token", token)
                .params("page", page)
                .params("limit", limit)
                .execute(new CacheCallBack<AddressListBean>() {
                    @Override
                    public void onSuccess(Response<AddressListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("地址：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onAddressListSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }

    //
    public void onAddressAddData(String name,
                                 String phone,
                                 int province,
                                 int city,
                                 int area,
                                 int street,
                                 String address,
                                 String type, int id) {
        LoggerUtils.e("canhsu ");
        OkGo.<MsgBean>post(Api.API_USER_ADDRESS_UP)
                .headers("token", LoginHelper.getLonginData().getResult())
                .params("name", name)
                .params("phone", phone)
                .params("province", province)
                .params("city", city)
                .params("area", area)
                .params("street", street)
                .params("address", address)
                .params("type", type)
                .params("id", id)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("地址add：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onAddressAddSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }

    public void onAddressAddData(
            String type, int id) {
        LoggerUtils.e("canhsu ");
        OkGo.<MsgBean>post(Api.API_USER_ADDRESS_UP)
                .headers("token", LoginHelper.getLonginData().getResult())
                .params("type", type)
                .params("id", id)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("地址add：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onAddressAddSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }
//
//    public void onAddressDeleteData(String time, String uuid, int ad_id, int type) {
//        OkGo.<MsgBean>post(Api.API_USER_ADDRESS_DELETE)
//                .headers("sign", RequestParamConfig.InitRequestParam(time))
//                .params("uuid", uuid)
//                .params("ad_id", ad_id)
//                .params("type", type)
//                .execute(new CacheCallBack<MsgBean>() {
//                    @Override
//                    public void onSuccess(Response<MsgBean> response) {
//                        super.onSuccess(response);
//                        LoggerUtils.e("地址：" + response.body());
//                        if (isViewAttached())
//                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
//                                getView().onAddressDeleteSuccess(response.body());
//                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
//                                getView().onReLoggedIn(response.body().getMessage());
//                            else
//                                getView().onError(response.body().getMessage());
//
//
//                    }
//                });
//    }


    //省
    public void onProvinceListData() {
        OkGo.<AddressProvinceListBean>get(Api.API_ADD_CITY)
                .headers("token", LoginHelper.getLonginData().getResult())
                .params("pid", 1)
                .execute(new CacheCallBack<AddressProvinceListBean>() {
                    @Override
                    public void onSuccess(Response<AddressProvinceListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("省：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onProvinceListSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }

    //市
    public void onCityListData(int pid) {
        OkGo.<AddressProvinceListBean>get(Api.API_ADD_CITY)
                .headers("token", LoginHelper.getLonginData().getResult())
                .params("pid", pid)
                .execute(new CacheCallBack<AddressProvinceListBean>() {
                    @Override
                    public void onSuccess(Response<AddressProvinceListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("市：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onCityListSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }

    public void onAreaListData(int pid) {
        OkGo.<AddressProvinceListBean>get(Api.API_ADD_CITY)
                .headers("token", LoginHelper.getLonginData().getResult())
                .params("pid", pid)
                .execute(new CacheCallBack<AddressProvinceListBean>() {
                    @Override
                    public void onSuccess(Response<AddressProvinceListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("市：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onCityListSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }
}
